﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using BEGDC.CC.BL.Common;
using BEGDC.CC.BL.Common.Query;
using BEGDC.CC.BL.UserManagement;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.Entity;
using BEGDC.CC.Web.UserControls;
using BEGDC.Utility.Castle;
using BEGDC.Utility.ExceptionManagement;
using NetBpm.Util.Client;
using NetBpm.Workflow.Execution;
using NetBpm.Workflow.Execution.EComp;

namespace BEGDC.CC.Web
{
    using ASBCC.Database.Service;
    using ASBCC.Business;

    /// <summary>
    /// Summary description for main.
    /// </summary>
    public class MyTask : System.Web.UI.Page
    {
        private const string SEARCH_CR = "CR";
        private const string SEARCH_EV = "EV";
        // All OCR
        private const string SEARCH_OCR = "OCR";
        // All OCV
        private const string SEARCH_OCV = "OCV";
        //the original OCR process
        private const string SEARCH_ORIGINAL_OCR = "ORIGINAL_OCR";
        //the original OCV process
        private const string SEARCH_ORIGINAL_OCV = "ORIGINAL_OCV";
        private const string SEARCH_EBDCR = "EBD";
        private const string SEARCH_ESCCR = "ESC";
        private const string SEARCH_EBDCV = "EBDCV";
        private const string SEARCH_ESCCV = "ESCCV";

        /// <summary>
        /// Gets a value indicating whether this instance is EZVisit request.
        /// </summary>
        protected bool IsEZVRequest
        {
            get
            {
                return this.Request.QueryString["topid"] == "10002";
            }
        }

        protected string flowtype
        {
            get
            {
                return this.Request.QueryString["flow"];
            }
        }

        /// <summary>
        ///
        /// </summary>
        private bool _IsSearch
        {
            get
            {
                if (ViewState["_IsSearch"] == null) return false;

                return Convert.ToBoolean(ViewState["_IsSearch"]);
            }
            set { ViewState["_IsSearch"] = value; }
        }

        /// <summary>
        ///
        /// </summary>
        private string _Sorting
        {
            get
            {
                if (ViewState["Sorting"] == null) return string.Empty;

                return ViewState["Sorting"].ToString();
            }
            set { ViewState["Sorting"] = value; }
        }

        protected System.Web.UI.WebControls.ImageButton btnReject;
        protected System.Web.UI.WebControls.DropDownList ddlDepartment;
        //protected System.Web.UI.WebControls.LinkButton lbtDelete;
        protected System.Web.UI.WebControls.ImageButton btnDelete;
        protected System.Web.UI.WebControls.TextBox txtApplication;
        protected System.Web.UI.WebControls.ImageButton btnPageFirst;
        protected System.Web.UI.WebControls.ImageButton btnPagePrev;
        protected System.Web.UI.WebControls.ImageButton btnPageNext;
        protected System.Web.UI.WebControls.ImageButton btnPageLast;
        protected System.Web.UI.WebControls.Label lblRecordCount;
        protected System.Web.UI.WebControls.Label lblPageCount;
        protected System.Web.UI.WebControls.Label lblCurrentPageIndex;
        protected System.Web.UI.WebControls.DataGrid dgTaskList;
        protected System.Web.UI.WebControls.TextBox txtPage;
        protected System.Web.UI.WebControls.ImageButton btnGotoPage;
        protected System.Web.UI.WebControls.CheckBox chkBatchApprove;

        protected System.Web.UI.WebControls.DropDownList ddlFlowType;

        //changed ImageButton by dev_leoren
        //protected System.Web.UI.WebControls.ImageButton btnApprove;
        //protected System.Web.UI.WebControls.ImageButton btnExport;
        //protected System.Web.UI.WebControls.ImageButton btnSearch;

        protected System.Web.UI.WebControls.Button btnApprove;
        protected System.Web.UI.WebControls.Button btnExport;
        protected System.Web.UI.WebControls.Button btnSearch;
        protected System.Web.UI.WebControls.Button btnNewRequest;

        protected System.Web.UI.HtmlControls.HtmlGenericControl tbTypeSpan;
        protected System.Web.UI.HtmlControls.HtmlGenericControl tbDdlFlowTypeSpan;

        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                // Put user code to initialize the page here
                if (!Page.IsPostBack)
                {
                    InitializeDropDownList();

                    this.btnApprove.Attributes.Add("onclick", "javascript:OnBatchApprove();");

                    this.btnNewRequest.Attributes.Add("onclick", "javascript:OpenNewRequest('" + flowtype + "');");

                    this.txtApplication.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + this.btnSearch.UniqueID + "').click();return false;}} else {return true}; ");

                    this.btnApprove.Visible = this.chkBatchApprove.Checked;

                    this.SetBatchApprove();

                    //if (!this.chkBatchApprove.Visible)
                    //{
                    //    this.dgTaskList.Columns[3].Visible = false;
                    //}

                    this.BindData(1);
                }
            }
            catch (GDCException gdcEx)
            {
                WebUtility.Go2ErrorPage(this.Page, gdcEx.ErrorNumber);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                WebUtility.Go2ErrorPage(this.Page, BEGDC.CC.BL.Common.ErrorCode.Other_Exception);
            }
        }


        private bool isExport;
        protected IUtility _service;
        protected IUtility Service
        {
            get
            {
                if (_service == null)
                    _service = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
                return _service;
            }
        }

        public string AppNoConverter(object appno)
        {
            return Service.GetCRNonB(appno.ToString());
        }


        private void BindOVFlowTypeDropDown()
        {
            //DropdownList ddlFlowType
            if (this.flowtype == null) return;

            if (this.flowtype.Equals(SEARCH_OCR) || this.flowtype.Equals(SEARCH_OCV))
            {
                this.tbTypeSpan.Visible = true;
                this.tbDdlFlowTypeSpan.Visible = true;

                if (this.flowtype.Contains(SEARCH_OCR))
                {
                    System.Web.UI.WebControls.ListItem itemOCR = new System.Web.UI.WebControls.ListItem("Overseas Customer Reception(海外客户来访接待)", SEARCH_ORIGINAL_OCR);
                    System.Web.UI.WebControls.ListItem itemEBD = new System.Web.UI.WebControls.ListItem("EBD Customer Reception(EBD海外客户来访接待)", SEARCH_EBDCR);
                    System.Web.UI.WebControls.ListItem itemESC = new System.Web.UI.WebControls.ListItem("ESC Customer Reception(ESC海外客户来访接待)", SEARCH_ESCCR);

                    this.ddlFlowType.Items.Add(itemOCR);
                    this.ddlFlowType.Items.Add(itemEBD);
                    this.ddlFlowType.Items.Add(itemESC);
                }
                else
                {
                    System.Web.UI.WebControls.ListItem itemOCV = new System.Web.UI.WebControls.ListItem("Overseas Customer Visiting(海外客户陪同出访)", SEARCH_ORIGINAL_OCV);
                    System.Web.UI.WebControls.ListItem itemEBDCV = new System.Web.UI.WebControls.ListItem("EBD Customer Visiting(EBD海外客户陪同出访)", SEARCH_EBDCV);
                    System.Web.UI.WebControls.ListItem itemESCCV = new System.Web.UI.WebControls.ListItem("ESC Customer Visiting(ESC海外客户陪同出访)", SEARCH_ESCCV);

                    this.ddlFlowType.Items.Add(itemOCV);
                    this.ddlFlowType.Items.Add(itemEBDCV);
                    this.ddlFlowType.Items.Add(itemESCCV);
                }
            }
        }

        private void SetBatchApprove()
        {
            BindOVFlowTypeDropDown();
            //System.Web.UI.WebControls.ListItem itemEV = new System.Web.UI.WebControls.ListItem("Executive Visiting(高层拜访)", "EV");
            //System.Web.UI.WebControls.ListItem itemCV = new System.Web.UI.WebControls.ListItem("Customer Domestic & Overseas Visiting(客户考察)", "CV");
            //System.Web.UI.WebControls.ListItem itemCR = new System.Web.UI.WebControls.ListItem("Customer Reception In Shanghai(客户接待)", "CR");
            //System.Web.UI.WebControls.ListItem itemCO = new System.Web.UI.WebControls.ListItem("Customer Reception Out Of Shanghai(上海以外客户接待)", "CO");
            //System.Web.UI.WebControls.ListItem itemOCR = new System.Web.UI.WebControls.ListItem("Overseas Customer Reception(海外客户来访接待)", "OCR");
            //System.Web.UI.WebControls.ListItem itemOCV = new System.Web.UI.WebControls.ListItem("Overseas Customer Visiting(海外客户陪同出访)", "OCV");
            //System.Web.UI.WebControls.ListItem itemEBD = new System.Web.UI.WebControls.ListItem("EBD Customer Reception(EBD海外客户来访接待)", "EBD");
            //System.Web.UI.WebControls.ListItem itemEBDCV = new System.Web.UI.WebControls.ListItem("EBD Customer Visiting(EBD海外客户陪同出访)", "EBDCV");
            //System.Web.UI.WebControls.ListItem itemESC = new System.Web.UI.WebControls.ListItem("ESC Customer Reception(ESC海外客户来访接待)", "ESC");
            //System.Web.UI.WebControls.ListItem itemESCCV = new System.Web.UI.WebControls.ListItem("ESC Customer Visiting(ESC海外客户陪同出访)", "ESCCV");

            //this.ddlFlowType.Items.Add(itemEV);
            //this.ddlFlowType.Items.Add(itemCV);
            //this.ddlFlowType.Items.Add(itemCR);
            //this.ddlFlowType.Items.Add(itemCO);
            //this.ddlFlowType.Items.Add(itemOCR);
            //this.ddlFlowType.Items.Add(itemOCV);
            //this.ddlFlowType.Items.Add(itemEBD);
            //this.ddlFlowType.Items.Add(itemEBDCV);
            //this.ddlFlowType.Items.Add(itemESC);
            //this.ddlFlowType.Items.Add(itemESCCV);

            //if (string.IsNullOrEmpty(this.Request.QueryString["topid"]))
            //{
            //    // Display EZV in the MyWorkspace tab.
            //    this.ddlFlowType.Items.Add(new ListItem("EZVisit", "EZV"));
            //}

            // Note: Currently the EZV does not support batch approve. So simply hide the functionality here.
            if (this.IsEZVRequest)
            {
                // Display only EZV in the EZVisit tab.
                //this.ddlFlowType.Items.Clear();
                //this.ddlFlowType.Items.Add(new ListItem("EZVisit", "EZV"));
                this.chkBatchApprove.Visible = this.btnApprove.Visible = false;
                return;
            }

            string roles = ((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).Roles;

            //			string roles = ((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).Roles;
            ////			if (roles.Split(",".ToCharArray()).Length > 2)
            ////			{
            ////				this.ddlFlowType.Items.Add(itemEV);
            ////				this.ddlFlowType.Items.Add(itemCV);
            ////				this.ddlFlowType.Items.Add(itemCR);
            ////				this.ddlFlowType.Items.Add(itemCO);
            ////				this.chkBatchApprove.Visible = true;
            ////				this.chkBatchApprove.Checked = false;
            ////				return;
            ////			}
            //
            //			this.chkBatchApprove.Visible = true;
            ////			this.btnApprove.Visible = true;
            //
            if (roles.IndexOf(BEGDC.CC.BL.Workflow.UserRole.CATDirector) > -1)
            {
                //				this.ddlFlowType.Items.Add(itemEV);
                //				this.ddlFlowType.Items.Add(itemCV);
                //				this.ddlFlowType.Items.Add(itemCR);
                this.chkBatchApprove.Visible = true;
                //this.chkBatchApprove.Checked = false;
                //this.btnApprove.Visible = true;
                return;
            }
            //
            if (roles.IndexOf(BEGDC.CC.BL.Workflow.UserRole.PSO) > -1)
            {
                //
                //				this.ddlFlowType.Items.Add(itemEV);
                //				this.ddlFlowType.Items.Add(itemCV);
                //				this.ddlFlowType.Items.Add(itemCR);
                this.chkBatchApprove.Visible = true;
                //this.chkBatchApprove.Checked = false;
                //this.btnApprove.Visible = true;
                return;
            }
            //
            if (roles.IndexOf(BEGDC.CC.BL.Workflow.UserRole.CODDirector) > -1)
            {
                //				this.ddlFlowType.Items.Add(itemCV);
                this.chkBatchApprove.Visible = true;
                //this.chkBatchApprove.Checked = false;
                //this.btnApprove.Visible = true;
                return;
            }
            //
            if (roles.IndexOf(BEGDC.CC.BL.Workflow.UserRole.CODVP) > -1)
            {
                //				this.ddlFlowType.Items.Add(itemCV);
                this.chkBatchApprove.Visible = true;
                //this.chkBatchApprove.Checked = false;
                //this.btnApprove.Visible = true;
                return;
            }
            //
            if (roles.IndexOf(BEGDC.CC.BL.Workflow.UserRole.DeptDirector) > -1)
            {
                //				this.ddlFlowType.Items.Add(itemCR);
                this.chkBatchApprove.Visible = true;
                //this.chkBatchApprove.Checked = false;
                //this.btnApprove.Visible = true;
                return;
            }
            //
            if (roles.IndexOf(BEGDC.CC.BL.Workflow.UserRole.DeptHead) > -1)
            {
                //				this.ddlFlowType.Items.Add(itemCR);
                this.chkBatchApprove.Visible = true;
                //this.chkBatchApprove.Checked = false;
                //this.btnApprove.Visible = true;
                return;
            }
            //
            if (roles.IndexOf(BEGDC.CC.BL.Workflow.UserRole.FNC) > -1)
            {
                //				this.ddlFlowType.Items.Add(itemCR);
                this.chkBatchApprove.Visible = true;
                //this.chkBatchApprove.Checked = false;
                //this.btnApprove.Visible = true;
                return;
            }
            //
            this.chkBatchApprove.Visible = false;
            //this.btnApprove.Visible = false;
            //
            //			this.ddlFlowType.Items.Add(itemEV);
            //			this.ddlFlowType.Items.Add(itemCV);
            //			this.ddlFlowType.Items.Add(itemCR);
            //			this.ddlFlowType.Items.Add(itemCO);
            //			this.chkBatchApprove.Visible = true;
        }

        private void InitializeDropDownList()
        {
            //Department
            IUser userService = AppContainer.Instance[ServiceConst.UserService] as IUser;
            this.ddlDepartment.DataSource = userService.GetBusinessGroupList();
            this.ddlDepartment.DataValueField = "ID";
            this.ddlDepartment.DataTextField = "DepartmentCode";
            this.ddlDepartment.DataBind();
            this.ddlDepartment.Items.Insert(0, new System.Web.UI.WebControls.ListItem("All", "-1"));
            this.ddlDepartment.SelectedIndex = 0;
        }

        private void BindData(int pageIndex)
        {
            //string flowtype = this.ddlFlowType.SelectedValue.ToString();

            //flowtype仅能区分EV、CR、OCV、OCR,
            //不能区分OCR下的Original OCR\EBDCR\ESCCR及OCV下的Original OCV\EBDCV\ESCCV
            //使用overSeasFlowType区分Original OCR\EBDCR\ESCCR、Original OCV\EBDCV\ESCCV
            string overSeasFlowType = this.flowtype;
            if (this.ddlFlowType.Visible)
            {
                if (!string.IsNullOrEmpty(this.ddlFlowType.SelectedValue.ToString()))
                {
                    overSeasFlowType = this.ddlFlowType.SelectedValue.ToString();
                }
            }

            //			if (Request.QueryString["type"] == null)
            //			{
            //				if (ddlFlowType.SelectedValue == "EV")
            //					this.btnApprove.Visible = true;
            //				else
            //					this.btnApprove.Visible = false;
            //			}

            bool isBacker = ((BEGDC.CC.BL.Workflow.CCIdentity)User.Identity).IsBackup;

            IQuery iquery = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
            System.Data.DataTable dtTasks = null;
            if (Request.QueryString["type"] != null)
            {
                this.chkBatchApprove.Checked = false;
                this.chkBatchApprove.Visible = false;
                this.btnApprove.Visible = false;
                this.btnNewRequest.Visible = true;
                dtTasks = iquery.QueryMyDraftList(((CCIdentity)HttpContext.Current.User.Identity).UserInfo.Id, overSeasFlowType);

                this.dgTaskList.Columns[3].Visible = false;
                //this.dgTaskList.Columns[11].Visible = false;
                this.dgTaskList.Columns[12].Visible = false;
                //this.dgTaskList.Columns[12].Visible = false;
                this.dgTaskList.Columns[13].Visible = false;
                //this.dgTaskList.Columns[13].Visible = false;
                this.dgTaskList.Columns[14].Visible = false;
                //this.dgTaskList.Columns[14].Visible = false;
                this.dgTaskList.Columns[4].Visible = false;
                this.dgTaskList.Columns[16].Visible = true;
                //this.dgTaskList.Columns.RemoveAt(14);
                this.dgTaskList.Columns.RemoveAt(4);
            }
            else
            {
                var user = ((CCIdentity)HttpContext.Current.User.Identity).UserInfo;

                if (this._IsSearch)
                {
                    dtTasks = iquery.QueryTaskList(user.Id, ((CCPrincipal)HttpContext.Current.User).Roles, overSeasFlowType, this.ddlDepartment.SelectedValue, this.txtApplication.Text.Trim(), isBacker);
                }
                else
                {
                    dtTasks = iquery.QueryTaskList(user.Id, ((CCPrincipal)HttpContext.Current.User).Roles, overSeasFlowType, isBacker);
                }

                if (flowtype.Length == 0 || flowtype.Equals("EZV"))
                {
                    var ezvTasks = iquery.QueryEZVTasks(user.NameEN, this._IsSearch ? this.txtApplication.Text.Trim() : string.Empty, this._IsSearch ? this.ddlDepartment.SelectedValue : "-1");

                    if (ezvTasks.Rows.Count != 0)
                    {
                        dtTasks.Merge(ezvTasks);
                    }
                }
            }

            if (overSeasFlowType == WorkflowTypes.CR)
               //this.dgTaskList.Columns[7].Visible = false;
                this.dgTaskList.Columns[8].Visible = false;

            if (this._Sorting != string.Empty)
            {
                dtTasks.DefaultView.Sort = this._Sorting;
            }

            if (this.chkBatchApprove.Checked)
            {
                dtTasks.DefaultView.RowFilter = "IsApproval=1 and StateCode <> 'Query'";
            }

            int count = dtTasks.DefaultView.Count;

            this.dgTaskList.DataSource = dtTasks.DefaultView;
            this.dgTaskList.DataBind();

            SetPageCount(count, pageIndex);
        }

        #region Web Form Designer generated code

        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.ddlFlowType.SelectedIndexChanged += new System.EventHandler(this.ddlFlowType_SelectedIndexChanged);
            this.chkBatchApprove.CheckedChanged += new System.EventHandler(this.chkBatchApprove_CheckedChanged);

            //changed ImageButton by dev_leoren
            //this.btnApprove.Click += new System.Web.UI.ImageClickEventHandler(this.btnApprove_Click);
            //this.btnExport.Click += new System.Web.UI.ImageClickEventHandler(this.btnExport_Click);
            //this.btnSearch.Click += new System.Web.UI.ImageClickEventHandler(this.btnSearch_Click);
            this.btnApprove.Click += new EventHandler(btnApprove_Click);
            this.btnExport.Click += new EventHandler(btnExport_Click);
            this.btnSearch.Click += new EventHandler(btnSearch_Click);

            this.btnPageFirst.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageFirst_Click);
            this.btnPagePrev.Click += new System.Web.UI.ImageClickEventHandler(this.btnPagePrev_Click);
            this.btnPageNext.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageNext_Click);
            this.btnPageLast.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageLast_Click);
            this.dgTaskList.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgTaskList_SortCommand);
            this.btnGotoPage.Click += new System.Web.UI.ImageClickEventHandler(this.btnGotoPage_Click);
            this.dgTaskList.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgTaskList_ItemDataBound);
            this.dgTaskList.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgTaskList_ItemCommand);
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion

        #region ---------- paging events ----------------

        private void btnPageFirst_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            this.dgTaskList.CurrentPageIndex = 0;
            this.BindData(1);
        }

        private void btnPagePrev_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (_CurrentPageIndex == 1) return;
            this.dgTaskList.CurrentPageIndex = _CurrentPageIndex - 2;
            this.BindData(_CurrentPageIndex - 1);
        }

        private void btnPageNext_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (_CurrentPageIndex == _PageCount) return;
            this.dgTaskList.CurrentPageIndex = _CurrentPageIndex;
            this.BindData(_CurrentPageIndex + 1);
        }

        private void btnPageLast_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            this.dgTaskList.CurrentPageIndex = _PageCount - 1;
            this.BindData(_PageCount);
        }

        private void btnGotoPage_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (this.txtPage.Text == string.Empty)
            {
                this.txtPage.Text = "1";
            }
            int gotopage = 1;
            try
            {
                gotopage = Convert.ToInt32(this.txtPage.Text);
            }
            catch { }
            int currentpage = 0;

            if (gotopage >= _PageCount)
            {
                gotopage = _PageCount;
                currentpage = gotopage - 1;
                if (currentpage < 0) currentpage = 0;
            }
            else if (gotopage <= 0)
            {
                gotopage = 1;
            }
            else
            {
                currentpage = gotopage - 1;
            }

            this.dgTaskList.CurrentPageIndex = currentpage;

            this.BindData(gotopage);

            this.txtPage.Text = string.Empty;
        }

        #endregion

        #region page navigation

        private int _PageCount
        {
            get
            {
                if (ViewState["PageCount"] != null) return (int)ViewState["PageCount"];
                else
                    return 0;
            }
            set
            {
                ViewState["PageCount"] = value;
            }
        }

        private int _CurrentPageIndex
        {
            get
            {
                if (ViewState["CurrentPageIndex"] != null) return (int)ViewState["CurrentPageIndex"];
                else
                    return 0;
            }
            set
            {
                ViewState["CurrentPageIndex"] = value;
            }
        }

        private int _RowCount
        {
            get
            {
                if (ViewState["RowCount"] != null) return (int)ViewState["RowCount"];
                else
                    return 0;
            }
            set
            {
                ViewState["RowCount"] = value;
            }
        }

        private void SetPageNavi(int newPage)
        {
            if (newPage <= 1)
            {
                this.btnPageFirst.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_first_inact.gif";
                this.btnPagePrev.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_pre_inact.gif";
                this.btnPageFirst.Attributes["disabled"] = "true";
                this.btnPagePrev.Attributes["disabled"] = "true";
            }
            else
            {
                this.btnPageFirst.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_first.gif";
                this.btnPagePrev.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_pre.gif";
                this.btnPageFirst.Attributes.Remove("disabled");
                this.btnPagePrev.Attributes.Remove("disabled");
            }

            if (newPage >= _PageCount)
            {
                this.btnPageNext.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_next_inact.gif";
                this.btnPageLast.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_last_inact.gif";
                this.btnPageNext.Attributes["disabled"] = "true";
                this.btnPageLast.Attributes["disabled"] = "true";
            }
            else
            {
                this.btnPageNext.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_next.gif";
                this.btnPageLast.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_last.gif";
                this.btnPageNext.Attributes.Remove("disabled");
                this.btnPageLast.Attributes.Remove("disabled");
            }

            this.lblCurrentPageIndex.Text = newPage.ToString();
            this.lblPageCount.Text = _PageCount.ToString();
            this.lblRecordCount.Text = _RowCount.ToString();
        }

        private void SetPageCount(int count, int pageIndex)
        {
            if (count > 0)
            {
                _RowCount = count;
                _PageCount = count / this.dgTaskList.PageSize;
                if (count % this.dgTaskList.PageSize > 0) _PageCount++;

                _CurrentPageIndex = pageIndex;
                SetPageNavi(pageIndex);
            }
            else
            {
                _RowCount = count;
                _PageCount = count;
                _CurrentPageIndex = 0;
                SetPageNavi(0);
            }
        }

        #endregion

        private void ddlFlowType_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            this.dgTaskList.CurrentPageIndex = 0;
            //this._IsSearch  = false;

            //			if (Request.QueryString["type"] == null)
            //			{
            //				if (ddlFlowType.SelectedValue == "EV")
            //					this.btnApprove.Visible = true;
            //				else
            //					this.btnApprove.Visible = false;
            //			}

            this.BindData(1);
        }

        void btnApprove_Click(object sender, EventArgs e)
        {
            string flowIds = "";
            foreach (DataGridItem item in this.dgTaskList.Items)
            {
                CheckBox chk = item.FindControl("chkSelection") as CheckBox;
                if (chk != null && chk.Checked)
                {
                    flowIds += item.Cells[0].Text + ";";
                }
            }
            flowIds = flowIds.TrimEnd(';');

            if (flowIds + string.Empty == string.Empty)
            {
                Response.Write("<script>alert('Please select tasks you want to approve !')</script>");
                return;
            }

            try
            {
                BEGDC.CC.BL.CCFlow.EV.IEV evService = AppContainer.Instance[ServiceConst.EVService] as BEGDC.CC.BL.CCFlow.EV.IEV;
                evService.BatchApprove(flowIds);

                this.dgTaskList.CurrentPageIndex = 0;
                this.BindData(1);
                Response.Write("<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "')</script>");
            }
            catch (BEGDC.Utility.ExceptionManagement.GDCException gdcEx)
            {
                Response.Write("<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "')</script>");
            }
        }

        //Added by Joe 2007-5-25 ======start=====>
        /// <summary>
        /// Approve EV Requests
        /// </summary>
        private void ApproveEVRequest()
        {
            string appIDs = string.Empty;
            foreach (DataGridItem item in this.dgTaskList.Items)
            {
                CheckBox chk = item.FindControl("chkSelection") as CheckBox;
                if (chk != null && chk.Checked)
                {
                    appIDs += item.Cells[1].Text + ";";
                }
            }
            appIDs = appIDs.Trim(';');

            if (!Page.IsClientScriptBlockRegistered("ApproveEV"))
            {
                StringBuilder script = new StringBuilder();
                script.Append("<script language=\"javasciript\" type=\"text\\javascript\">\n");
                script.Append(" window.showModalDialog('../UserControls/EV/AssignApps.aspx?AppIDs=" + appIDs + "'," + "'', 'dialogHeight=150px,dialogWidth=400px')" + ";\n");
                script.Append("</script>\n");
                string scriptCMD = script.ToString();

                RegisterClientScriptBlock("ApproveEV", scriptCMD);
            }
        }

        //======>end======

        //private void btnExport_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        //{
        //    this.dgTaskList.AllowPaging = false;
        //    this.BindData(1);
        //    BEGDC.CC.Web.Control.ExportDataGridToExcel.Export(Page, dgTaskList, "MyActivity.xls");
        //}

        void btnExport_Click(object sender, EventArgs e)
        {
            this.isExport = true;

            this.dgTaskList.AllowPaging = false;

            this.BindData(1);

            this.dgTaskList.Columns[15].Visible = false;

            BEGDC.CC.Web.Control.ExportDataGridToExcel.Export(Page, dgTaskList, "MyActivity.xls");
            this.dgTaskList.Columns[15].Visible = true;
        }

        void btnSearch_Click(object sender, EventArgs e)
        {
            this.dgTaskList.CurrentPageIndex = 0;
            this.txtApplication.Text = PageSafeHelper.SafeText(this.txtApplication.Text);
            if (this.ddlDepartment.SelectedIndex == 0 && this.txtApplication.Text.Trim().Length == 0)
            {
                this._IsSearch = false;
                this.BindData(1);
                return;
            }
            this._IsSearch = true;

            this.BindData(1);
        }

        public void dgTaskList_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            if (this._Sorting.IndexOf(e.SortExpression) > -1 && this._Sorting == e.SortExpression)
                this._Sorting = e.SortExpression + " desc";
            else
                this._Sorting = e.SortExpression;
            this.BindData(this._CurrentPageIndex);
        }

        private void chkBatchApprove_CheckedChanged(object sender, System.EventArgs e)
        {
            this.dgTaskList.CurrentPageIndex = 0;
            this.btnApprove.Visible = chkBatchApprove.Checked;
            this.dgTaskList.Columns[3].Visible = chkBatchApprove.Checked;
            this.BindData(1);
        }

        public bool IsAccessActionPoint()
        {
            //string filename = System.IO.Path.GetFileName(Request.FilePath);
            return ((BEGDC.CC.BL.Workflow.CCPrincipal)HttpContext.Current.User).IsAccess("myap.aspx");
        }

        private void dgTaskList_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            this.ItemBind(e, "lblStatus", "btnDelete");
            if (isExport)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    string state = DataBinder.Eval(e.Item.DataItem, "StateCode").ToString();
                    if (state == "Callback")
                        state = "Drawback";
                    e.Item.Cells[13].Text = state;
                }
            }
        }

        private void ItemBind(System.Web.UI.WebControls.DataGridItemEventArgs e, string statusName, string deleteName)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lbl = (Label)e.Item.FindControl(statusName);
                var btnDelete = (ImageButton)e.Item.FindControl(deleteName);
                string status = lbl.Text;
                if (status == "Reject" || status == string.Empty)
                {
                    btnDelete.Visible = true;
                    btnDelete.Attributes.Add("onclick", "if(!confirm('Are you sure to delete the application?')) return false;");
                }

                else
                {
                    btnDelete.Visible = false;
                }
            }
        }

        private void dgTaskList_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                bool ret = false;
                int appId = int.Parse(e.Item.Cells[1].Text);

                if (e.Item.Cells[17].Text.Equals("EZV"))
                {
                    var app = EZVisitApplicationManager.SelectByKeys(appId);

                    if (app != null)
                    {
                        app.IsEnabled = false;
                        ret = EZVisitApplicationManager.Update(app) == 1;
                    }
                }
                else
                {
                    BEGDC.CC.BL.Common.UtilityService utiService = AppContainer.Instance[ServiceConst.UtilityService] as BEGDC.CC.BL.Common.UtilityService;
                    if (Request.QueryString["type"] != null)
                    {
                        ret = utiService.DeleteDraftApplication(appId);
                    }
                    else
                    {
                        ret = utiService.DeleteApplication(appId);
                    }
                }

                if (ret)
                {
                    Page.RegisterClientScriptBlock("", "<script>alert('Application delete successfully!');</script>");
                }
                else
                {
                    Page.RegisterClientScriptBlock("", "<script>alert('Application delete failed!');</script>");
                }
                this.BindData(1);
            }
        }
    }
}